from django.shortcuts import render,HttpResponse
from . import models
import time
from django.contrib.auth.decorators import login_required
from django.contrib import auth


# Create your views here.
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        # print(username,password)
        user_obj = models.State.objects.filter(state_user=username,state_pwd=password).first()
        # print(user_obj)
        jtime = time.asctime( time.localtime(time.time()) )
        if user_obj:
            # auth.login(username)
            print('登录成功')
            return render(request, 'home.html',locals())

    return render(request, 'statelogin.html')


@login_required
def home(request):
    jtime = time.asctime(time.localtime(time.time()))
    return render(request, 'home.html', locals())
"""
urls.py
    path(r'room_info/',views.room_info),    #房间信息
    path(r'guid_info/',views.guid_info),    #引导员信息
    path(r'kaccount_info/',views.kaccount_info),#k歌账号
    path(r'help_info/',views.help_info),    #帮助文档
"""


def room_info(request):
    room_querys = models.Room.objects.order_by('room_state').all()
    for room_query in room_querys:
        # room_query.room_time=time.asctime( time.localtime(time.time()) )
        if room_query.room_state=="f":
          room_query.room_state="未使用"
        else:
            room_query.room_state="已使用"
    return render(request,"room_info.html",locals())


def guid_info(request):
    guid_querys = models.Guide.objects.order_by('guide_state').all()
    for guid_query in guid_querys:
        # room_query.room_time=time.asctime( time.localtime(time.time()) )
        if guid_query.guide_state=="f":
          guid_query.guide_state="未使用"
        else:
            guid_query.guide_state="已使用"
        if guid_query.guide_gender=="f":
          guid_query.guide_gender="女生"
        else:
            guid_query.guide_gender="男生"
    return render(request,"guid_info.html",locals())


def kaccount_info(request):
    kaccount_querys = models.Account.objects.order_by('account_state').all()
    for kaccount_query in kaccount_querys:
        # room_query.room_time=time.asctime( time.localtime(time.time()) )
        if kaccount_query.account_state=="f":
          kaccount_query.account_state="未使用"
        else:
            kaccount_query.account_state="已使用"
    return render(request,"kaccount_info.html",locals())


def help_info(request):
    return render(request,"help_info.html")

def work(request):
    # room_querys = models.Room.objects.order_by('room_state').all()
    # guid_querys = models.Guide.objects.order_by('guide_state').all()
    # kaccount_querys = models.Account.objects.order_by('account_state').all()
    room_querys = models.Room.objects.filter(room_state__exact='f')
    guid_querys = models.Guide.objects.filter(guide_state__exact='f')
    kaccount_querys = models.Account.objects.filter(account_state__exact='f')
    return render(request,"home_work.html", locals())

    # path(r'work_release/',views.work_release),# 提交订单
    # path(r'work_place/',views.work_place),#释放订单
# 提交订单


def work_place(request):
    if request.method == 'POST':
        # place_room = 99
        # place_account = 99
        # place_guide = 99
        place_room = request.POST.get('room_id')
        place_time = request.POST.get('room_time')
        place_account = request.POST.get('account_id')
        place_guide = request.POST.get('guide_id')
        # print(place_room,place_account,place_guide)
        import datetime
        tmie_h = float(place_time)
        time = datetime.datetime.now() + datetime.timedelta(hours=tmie_h)
        # print(time)
    models.Room.objects.filter(pk=place_room).update(room_state='t')
    models.Room.objects.filter(pk=place_room).update(room_time=time)
    models.Account.objects.filter(pk=place_account).update(account_state='t')
    guide = models.Guide.objects.get(pk=place_guide)
    guide.guide_state='t'
    guide.guide_frequency=guide.guide_frequency+1
    guide.save()
    # models.Guide.objects.filter(pk=place_guide).update(guide_state='t')
    # models.Guide.objects.filter(pk=place_guide).update(guide_frequency='t')
    return work(request)

#释放订单


def work_release(request):
    if request.method == 'POST':
        # place_room = 99
        # place_account = 99
        # place_guide = 99
        place_room = request.POST.get('room_id')
        place_account = request.POST.get('account_id')
        place_guide = request.POST.get('guide_id')
        print(place_room, place_account, place_guide)
    if place_room !='' :
        models.Room.objects.filter(pk=place_room).update(room_state='f')
    if place_account !='' :
        models.Account.objects.filter(pk=place_account).update(account_state='f')
    if place_guide !='' :
        models.Guide.objects.filter(pk=place_guide).update(guide_state='f')
    return work(request)